package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.ProductBomDetail;
import com.uwlaser.service.ProductBomDetailService;
import com.uwlaser.service.dto.ProductBomDetailDto;
import com.uwlaser.service.dto.ProductBomDetailQueryParam;
import com.uwlaser.service.mapper.ProductBomDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = ProductBomDetailService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProductBomDetailServiceImpl extends CommonServiceImpl<ProductBomDetailMapper, ProductBomDetail> implements ProductBomDetailService {

    // private final RedisUtils redisUtils;
    private final ProductBomDetailMapper productBomDetailMapper;

    @Override
    public PageInfo<ProductBomDetailDto> queryAll(ProductBomDetailQueryParam query, Pageable pageable) {
        IPage<ProductBomDetail> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<ProductBomDetail> page = productBomDetailMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, ProductBomDetailDto.class);
    }

    @Override
    public List<ProductBomDetailDto> queryAll(ProductBomDetailQueryParam query){
        return ConvertUtil.convertList(productBomDetailMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), ProductBomDetailDto.class);
    }

    @Override
    public ProductBomDetail getById(Integer id) {
        return productBomDetailMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public ProductBomDetailDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), ProductBomDetailDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(ProductBomDetailDto resources) {
        ProductBomDetail entity = ConvertUtil.convert(resources, ProductBomDetail.class);
        ProductBomDetail productBomDetail = new ProductBomDetail();
        productBomDetail.setBomId(entity.getBomId());
        productBomDetail.setOperationCode(entity.getOperationCode());
        productBomDetail.setMaterialCode(entity.getMaterialCode());
        Integer integer = productBomDetailMapper.selectCount(new QueryWrapper<>(productBomDetail));
        if(integer > 0){
            throw  new RuntimeException("物料编码【"+entity.getMaterialCode()+"】在该工序已配置！");
        }
        return productBomDetailMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(ProductBomDetailDto resources){
        ProductBomDetail entity = ConvertUtil.convert(resources, ProductBomDetail.class);
        int ret = productBomDetailMapper.updateById(entity);
        ProductBomDetail productBomDetail = new ProductBomDetail();
        productBomDetail.setBomId(entity.getBomId());
        productBomDetail.setOperationCode(entity.getOperationCode());
        productBomDetail.setMaterialCode(entity.getMaterialCode());
        Integer integer = productBomDetailMapper.selectCount(new QueryWrapper<>(productBomDetail));
        if(integer >= 2){
            throw  new RuntimeException("物料编码【"+entity.getMaterialCode()+"】在该工序已配置！");
        }
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return productBomDetailMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<ProductBomDetailDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (ProductBomDetailDto productBomDetail : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("清单ID", productBomDetail.getBomId());
              map.put("物料编码", productBomDetail.getMaterialCode());
              map.put("工序编号", productBomDetail.getOperationCode());
              map.put("用量", productBomDetail.getQuantity());
              map.put("损耗率", productBomDetail.getAttritionRate());
              map.put("编码类型 ", productBomDetail.getCodingType());
              map.put("是否必传", productBomDetail.getIsNecessary());
              map.put("描述", productBomDetail.getRemark());
              map.put("创建人", productBomDetail.getCreateBy());
              map.put("创建时间", productBomDetail.getCreateTime());
              map.put("更新人", productBomDetail.getUpdateBy());
              map.put("更新时间", productBomDetail.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
